package com.yihecloud.client;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONObject;

import com.pubframework.common.http.HttpUtil;
import com.pubframework.common.util.datetime.DateUtil;
 
public class RestClientDemo {

	static int count = 0;
	
	public static String getBaseURI() {
//		return "http://192.168.10.37:12315";
		return "http://127.0.0.1:8889";
	} 

	public static void main(String[] args) {
//		testApi("testQuery");
		testApi("testInsert");
		//testApi("testDelete");
//		testApi("testUpdate");
//		testApi("testQueryPage");
//		testApi("testGetChildDataDic");
//		testApi("testGetDataDicTree");
	}
	
	public static void testApi(String methodName) {
		count++;
		System.out.println(count + "、测试api[" + methodName + "]\n");

		try {
			Method method = RestClientDemo.class.getMethod(methodName);
			method.invoke(RestClientDemo.class);
			System.out.println("\n接口已经联通");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public static void testQuery() throws IOException {
		String vo = HttpUtil.get(getBaseURI()+"/datadic/query/2551bfcc2cfd4149b9913435cd46ba5f", "utf-8");
		System.out.println(JSONObject.fromObject(vo).toString());
	} 
	public static void testGetDataDicTree() throws IOException {
		Map<String, String> paramMap = new HashMap<String ,String>();
//		paramMap.put("sysCode", "de5c5620c568422b8faba622d5d1bdf3");
		paramMap.put("dicPcode", "1");
		String vo = HttpUtil.get(getBaseURI()+"/datadic/getDataDicTree",paramMap );
		System.out.println(JSONObject.fromObject(vo).toString());
	} 
	public static void testGetChildDataDic() throws IOException {
		Map<String, String> paramMap = new HashMap<String ,String>();
//		paramMap.put("sysCode", "de5c5620c568422b8faba622d5d1bdf3");
		paramMap.put("dicPcode", "1");
		String vo = HttpUtil.get(getBaseURI()+"/datadic/getChildDataDic",paramMap );
		System.out.println(JSONObject.fromObject(vo).toString());
	} 
	
	public static void testInsert() throws IOException {
		Map<String, Object> paramMap = new HashMap<String ,Object>();
		
//		paramMap.put("dcCode", "rest1");
		paramMap.put("dcCreateTime", DateUtil.now());
		paramMap.put("dcCreateUser", "rest");
		paramMap.put("dcId", "rest测试");
		paramMap.put("dcIsDeleted", "0");
		paramMap.put("dcLastUpdateTime", DateUtil.now());
		paramMap.put("dcName", "rest测试");
		paramMap.put("dcPcode", "1_2");
		paramMap.put("dcRemark", "rest测试");
		paramMap.put("dcSysCode", "rest测试");
		
		String vo = HttpUtil.postFormParams(getBaseURI()+"/datadic/", "add", paramMap );
		System.out.println(JSONObject.fromObject(vo).toString());
	} 
	
	public static void testUpdate() throws IOException {
		Map<String, Object> paramMap = new HashMap<String ,Object>();
		/**
		 * String dcId, String dcCode, String dcName,
			String dcPcode, String dcSysCode, String dcSysName,
			String dcIsDeleted, String dcCreateTime, String dcLastUpdateTime,
			String dcRemark, String dcCreateUser
		 */
//		paramMap.put("dcCode", "zhao1");
//		paramMap.put("dcCreateTime", DateUtil.now());
		paramMap.put("dcCreateUser", "zhaouser");
		paramMap.put("dicId", "075fbcf52fe04595a6451b883cf4a6d5");
//		paramMap.put("dcIsDeleted", "0");
//		paramMap.put("dcLastUpdateTime", DateUtil.now());
//		paramMap.put("dcName", "zhaoname");
//		paramMap.put("dcPcode", "rest测试");
//		paramMap.put("dcRemark", "rest测试");
		paramMap.put("dcSysCode", "zhaodcSysCode");
		paramMap.put("dcSysName", "zhaosysname");
		
		String vo = HttpUtil.postFormParams(getBaseURI()+"/datadic/", "update", paramMap );
		System.out.println(JSONObject.fromObject(vo).toString());
	} 
	
	public static void testDelete() throws IOException{
		Map<String, Object> paramMap = new HashMap<String ,Object>();
		paramMap.put("dicId", "2551bfcc2cfd4149b9913435cd46ba5f");
		String vo = HttpUtil.postFormParams(getBaseURI()+"/datadic/", "delete", paramMap  );
		System.out.println(JSONObject.fromObject(vo).toString());
	}
	
	public static void testQueryPage() throws IOException{
		com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
		jsonObject.put("dcSysCode", "demo");
		jsonObject.put("dcPcode", "87");
//		jsonObject.put("dicId", "测试");
		//{"dcPcode":"87","dcSysCode":"demo"}
//		String param = "{dcPcode:'87',dcSysCode:'demo'}";
		Map<String, Object> paramMap = new HashMap<String ,Object>();
		paramMap.put("conditions", jsonObject.toJSONString());
		paramMap.put("pageNo", "1");
		paramMap.put("pageSize", "10");
		
		String vo = HttpUtil.post(getBaseURI()+"/datadic/query", paramMap);
		System.out.println(JSONObject.fromObject(vo).toString());
	}
}
